/**
 * Date:2021年3月15日 上午10:24:47
 * Copyright (c) 2021, www.bwbroad.com All Rights Reserved
 */

package com.app.rabbit;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * 
 * Description: 参考 https://blog.csdn.net/qq_36357242/article/details/107686056
 * <br/>
 * Date:2021年3月31日 下午3:16:31 <br/>
 * 
 * @author xuejianxin
 */
@Component
public class DirectProducer {

	private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DirectProducer.class);

	@Autowired
	private RabbitTemplate rabbitTemplate;

	// 声明路由模式交换机
	@Bean
	public DirectExchange direct() {
		return new DirectExchange("exchange.direct");
	}

	// 申明俩个路由模式队列
	@Bean
	public Queue mailQueue() {
		return new Queue("mailQueue"); // 队列一
	}

	@Bean
	public Queue smsQueue() {
		return new Queue("smsQueue"); // 队列二
	}

	// 将队列队列一绑定到交换机
	@Bean
	public Binding directBindingMail(DirectExchange direct, Queue mailQueue) {
		return BindingBuilder.bind(mailQueue).to(direct).with("mail");
	}

	// 将队列队列二绑定到交换机
	@Bean
	public Binding directBindingSms(DirectExchange direct, Queue smsQueue) {
		return BindingBuilder.bind(smsQueue).to(direct).with("sms");
	}

	// 创建消息发送者
	public void sendToDirect(String routeKey, String msg) {
		String context = "exchange.direct发送的消息:" + msg;
		this.rabbitTemplate.convertAndSend("exchange.direct", routeKey, context);
		log.info("sendToDirect exchange.direct routeKey={},msg={} ",routeKey,msg);
	}

}
